package com.sky.course.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.sky.course.entity.Course;
import com.sky.course.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Description:    课程服务端
* @Author:         Joe
* @CreateDate:     2019/10/24 11:39
*/
@RestController
@RequestMapping(value = "/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @RequestMapping(value = "/get/{cid}",method = RequestMethod.GET)
    //一旦调用服务方法失败并抛出异常信息后，会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法
    @HystrixCommand(fallbackMethod = "processHystrix_Get")
    public Course get(@PathVariable("cid")Integer cid){
        Course course = courseService.get(cid);
        if(null == course){
            throw new RuntimeException("该ID："+ cid + "没有对应的信息");
        }
        return course;
    }

    public Course processHystrix_Get(@PathVariable("cid")Integer cid){
        return new Course().setCId(cid).setCName("该ID："+ cid + "没有对应的信息,null--@HystrixCommand")
                .setDbSource("no this database in mysql");
    }


}
